package com.cauli.file.utils;

import com.cauli.file.component.FileComponent;
import com.cauli.file.model.entity.UserFile;

/**
 * @author Cauli
 * @date 2023/3/18 17:56
 * @description 用户文件 工具类
 */
public class UserFileUtils {

    public static UserFile getUserDir(Long userId, String filePath, String fileName) {
        UserFile userFile = new UserFile();
        userFile.setUserId(userId);
        userFile.setFileId(null);
        userFile.setFileName(fileName);
        userFile.setFilePath(FileComponent.formatPath(filePath));
        userFile.setExtendName(null);
        userFile.setIsDir(1);
        userFile.setDeleteBatchNum(null);
        return userFile;
    }

    public static UserFile getUserFile(Long userId, Long fileId, String filePath, String fileName, String extendName) {
        UserFile userFile = new UserFile();
        userFile.setUserId(userId);
        userFile.setFileId(fileId);
        userFile.setFileName(fileName);
        userFile.setFilePath(FileComponent.formatPath(filePath));
        userFile.setExtendName(extendName);
        userFile.setIsDir(0);
        userFile.setDeleteBatchNum(null);
        return userFile;
    }

    public static UserFile searchUserFileParam(UserFile userFile) {
        UserFile param = new UserFile();
        param.setFilePath(FileComponent.formatPath(userFile.getFilePath()));
        param.setFileName(userFile.getFileName());
        param.setExtendName(userFile.getExtendName());
        param.setUserId(userFile.getUserId());
        param.setIsDir(0);
        return param;
    }

    public static String formatLikePath(String filePath) {
        String newFilePath = filePath.replace("'", "\\'");
        newFilePath = newFilePath.replace("%", "\\%");
        newFilePath = newFilePath.replace("_", "\\_");
        return newFilePath;
    }
}
